<?php
/**
 * 	Bushido
 *
 * @category   Bushido
 * @package    Bushido
 * @copyright  Copyright (c) 2008-2009 Nathan Keyes
 * @author Nathan Keyes
 * @version    $Id: ClassType.php 7 2009-11-14 22:42:08Z nkeyes $
 */

class Bushido_DbTable_ClassType extends Bushido_Db_Table_Abstract
{
	protected $_name = 'class_type';
	
	protected $_sequence = true;
	
	
	protected $_referenceMap = array
	(
	);
	
	protected $_childMap = array
	(
		'ClassFieldTypes' => array
		(
			'refTableClass' => 'Bushido_DbTable_ClassFieldType',
			'cardinality' => Bushido_Db_Table_Abstract::ONE_OR_MANY
		),
	);
	
	
	/**
	 * (non-PHPdoc)
	 * @see library/Bushido/Db/Table/Bushido_Db_Table_Abstract#_setFieldDetails()
	 */
	protected function _setFieldDetails()
	{
		//name
		$this->_metadata['name']['fieldDetails'] = array
		(
			'options' => array
			(
				'label' => 'Name:',
				'required' => true,
				'invalidMessage' => 'Please enter a Name.'
			) 
		);
	}
	
	/**
	 * (non-PHPdoc)
	 * @see library/Bushido/Db/Table/Bushido_Db_Table_Abstract#getDisplayValue()
	 */
	public static function getDisplayValue(Zend_Db_Table_Row_Abstract $row, $context = null)
	{
		return $row->name;
	}
	
	/**
	 * 
	 * @param $classType
	 * @return Bushido_Form
	 */
	public static function getInstanceForm($classType)
	{
		$classInstance = null;
		if(!$classType instanceof Bushido_Db_Table_Row)
		{
			//Zend_Debug::dump($classType, 'not a row:');
			$classType = Bushido_Db_Table::getTable('Bushido_DbTable_ClassType')->find($classType)->current();
		}
		elseif($classType->getTable() instanceof Bushido_DbTable_ClassInstance)
		{
			$classInstance = $classType;
			$classType = $classType->findParentBushido_DbTable_ClassType();
		}
		$form = new Bushido_Form();
		
		$form->addElement('hidden', 'class_type_id', array('value' => $classType->class_type_id));
		$fields = $classType->findBushido_DbTable_ClassFieldType(Bushido_Db_Table::getTable('Bushido_DbTable_ClassFieldType')->select()->order('order ASC'));
		foreach($fields as $field)
		{			
			$form->generateElement('field_type_2_class_type_' . $field->field_type_2_class_type_id, self::_getFieldData($field));
		}
		if($classInstance)
		{
			$form->addElement('hidden', 'class_instance_id', array('value' => $classInstance->class_instance_id));
			$classInstanceDataArray = self::getInstanceDataArray($classInstance);
			//Zend_Debug::dump($classInstanceDataArray);
			$form->populate($classInstanceDataArray);
		}
		return $form;
	}
	
	protected static function _getFieldData(Bushido_Db_Table_Row $field)
	{
		$fieldData = array
		(
			'DATA_TYPE' => Bushido_Db_Table::getTable('Bushido_DbTable_FieldType')->find($field->field_type_id)->current()->name,
			'fieldDetails' => array
			(
				'options' => array
				(
					'label' => $field->label
				)
			),
		);
		return $fieldData;
	}
	
	public static function getInstanceDataArray($classInstance)
	{
		if(!$classInstance instanceof Bushido_Db_Table_Row)
		{
			$classInstance = Bushido_Db_Table::getTable('Bushido_DbTable_ClassInstance')->find($classInstance)->current();
		}
		$classType = $classInstance->findParentBushido_DbTable_ClassType();
		$dataItemArray = array
		(
			'class_instance_id' => $classInstance->class_instance_id
		);
		$classFieldTypes = $classType->findBushido_DbTable_ClassFieldType(Bushido_Db_Table::getTable('Bushido_DbTable_ClassFieldType')->select()->order('order ASC'));
		foreach($classFieldTypes as $classFieldType)
		{
			$fieldType = $classFieldType->findParentBushido_DbTable_FieldType();
			$instanceFieldTableName = Bushido_Db_Table::getAdapter()->fetchOne('SELECT name FROM field_type where field_type_id = ?', $classFieldType->field_type_id);
			$instanceFieldTable = new Bushido_Db_Table_Generic(array('name' => $instanceFieldTableName . '_field', 'sequence' => true));
			
			//$dataItemArray[$classFieldType->label] = $instanceFieldTable
			$dataItemArray['field_type_2_class_type_' . $classFieldType->field_type_2_class_type_id] = $instanceFieldTable
			->fetchAll
			(
				$instanceFieldTable->select()
				->where('class_instance_id = ?', $classInstance->class_instance_id)
				->where('field_type_2_class_type_id = ?', $classFieldType->field_type_2_class_type_id)
			)->current()->value;
		}
		return $dataItemArray;
	}
}